package com.ztesoft.sca.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Component;

import com.ztesoft.sca.model.DataSetModel;

@Component
public class DataSetDaoImpl implements DataSetDao{
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public DataSetDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    @Override
	public void addDataSet(List<DataSetModel> list)
    { 
    	NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
    	 SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
    	 String sql = "INSERT INTO SCA_DATASET(set_id,set_name,set_domain,set_folder,set_role_arn,channel_type,create_type) "
    	 		+ "VALUES (:setId,:setName,:setDomain,:setFolder,:setRoleArn,:channelType,:createType)";
    	 namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

	@Override
	public List<DataSetModel> selectDataSetList() {
        String sqlStr="select set_id,set_name,set_domain,set_folder,set_role_arn,channel_type,create_type from SCA_DATASET";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        Object[] param = new Object[]{};
        List<DataSetModel> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(DataSetModel.class));
        return list;
	}

	@Override
	public void updateDataSet(List<DataSetModel> listUpdate) {
	     NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
	   	 SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(listUpdate.toArray());
	   	 String sql = "UPDATE SCA_DATASET SET set_name=:setName,set_domain=:setDomain,"
	   	 		+ "set_folder=:setFolder,set_role_arn=:setRoleArn,channel_type=:channelType,create_type=:createType where set_id=:setId";
	   	 namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
	}

}
